package org.myy.learn.hot.finish.滑动窗口;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 字符串中所有字符异位词 {
    public static List<Integer> findAnagrams(String s, String p) {
        int slen = s.length(), plen = p.length();
        List<Integer> ans = new ArrayList<>();
        if (slen < plen){return ans;}
        int[] sArr = new int[26], pArr = new int[26];
        for(int i = 0; i < plen; i++){
            pArr[p.charAt(i) - 'a']++;
        }
        //todo 开始滑动
        for (int left = 0, right = 0; right < slen; right++){
            sArr[s.charAt(right) - 'a']++;
            if(right - left + 1 == plen){
                if (Arrays.equals(sArr, pArr)){
                    ans.add(left);
                }
                sArr[s.charAt(left) - 'a']--; //移走
                left++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        String s = "cbaebabacd";
        String p = "abc";
        System.out.println(findAnagrams(s, p));
    }
}
